<?php
//error_reporting(E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR);
require('./roots.php');
require($root_path.'include/inc_environment_global.php');

/**
* CARE2X Integrated Hospital Information System beta 2.0.1 - 2004-07-04
* GNU General Public License
* Copyright 2002,2003,2004,2005,2006 Elpidio Latorilla
* elpidio@care2x.org, 
*
* See the file "copy_notice.txt" for the licence notice
*/
$lang_tables=array('interconsulta.php');
define('LANG_FILE','history.php');
define('NO_2LEVEL_CHK',1);
require_once($root_path.'include/inc_front_chain_lang.php');

if(isset($_SESSION['roles'])){

$update = $_GET['update'];

if (!isset($update))
{
    $update=0;
}

$thisfile=basename(__FILE__);
$breakfile=$root_path.'modules/registration_admission/registration_admission.php';
$action='historia_clinica-save.php'.URL_APPEND ."&lang=$lang&update=$update";

require_once($root_path.'include/care_api_classes/class_core.php');
$core=new Core;

require_once($root_path.'include/care_api_classes/class_gui_history.php');

$ficha=$_GET['ficha'];
$pid=$_GET['pid'];
$sub = $_GET['sub'];
$corr = $_GET['id_correlativo'];
if( ($pid==NULL)&&($ficha!=NULL)){
	header ("Location: historia_clinica.php?sid=$sid&lang=$lang"); 
}
$s=0;




if (!isset($corr) || empty($corr))
{
    $corr = 0;
}
require_once($root_path.'include/inc_css_a_hilitebu.php');
include_once('../../include/inc_date_format_functions.php');
require_once ('../../js/jscalendar/calendar.php');
$calendar = new DHTML_Calendar('../../js/jscalendar/', $lang, 'calendar-system', true);
$calendar->load_files();
$sTemp2 = ob_get_contents();
ob_end_clean();




$guihistory = new GuiHistory($ficha);
if (!($campos = $guihistory->getAllInfoObject($ficha, $pid, $corr))) {

 }



/*  echo '<pre>';
  echo "HOLA";
 
  echo '</pre>';
*/
//var_dump($campos);
$i=0;
while ($campos[$i]!=NULL)
{
    $temp=$campos[$i];
    if (!isset($sub) && strlen($temp['subficha']))
    {
            $campos = $guihistory->getAllInfoObjectBySubficha($ficha, $temp['subficha'], $pid, $corr);
//             echo '<pre>';
             //echo "CHAO_no_sub";
//             var_dump($campos);
//             echo '</pre>';
            $i=0;
            $s = 1;
            break;
    }
    else if (isset($sub))
    {
        $campos = $guihistory->getAllInfoObjectBySubficha($ficha, $sub, $pid, $corr);
//         echo '<pre>';
             //echo "VAYA_consub";
               //var_dump($campos);
//             echo '</pre>';
        $s = 1;
        break;
    }
    else
    {
      //echo("<br>intento meter algo a gui[] antes de sub<br>");
      $gui[] .= $guihistory->createOpt($temp,$calendar,$pid, (($ficha == 'antecedentes')?(false):($update)) /* to see if we should block fields */);
        
        if ($temp['obligatorio'])
        {
            $oblig[] = $temp['nombre'];
        }
    }
	$i++;
 	
}

if ($s == 1)
{
        $i=0;
        //echo("<br>aqui vamos a que s==1<br>");
        //var_dump($campos);
	while ($campos[$i]!=NULL)
    {   
        $temp=$campos[$i];
		$tmp_opt = '';
        if (strlen($temp['subsubficha']) && strcmp($modo, $temp['subsubficha']))
        {
            $modo = $temp['subsubficha'];
            $tmp_opt = '<tr><td colspan="2">' . $LD{$temp['subsubficha']} . '</td></tr>';
        }
        //echo("<br>intento meter algo a gui[] post sub<br>");
        $gui[] .= $tmp_opt . $guihistory->createOpt($temp,$calendar,$pid, (($ficha == 'antecedentes')?(false):($update)) /*to see if we should block fields */);
        if ($temp['obligatorio'])
        {
            $oblig[] = $temp['nombre'];
        }
		$i++;
    }
}
ob_start();

?>
                <script  language="javascript">

                function checknum(elemento)
                {
                    var anum=/(^\d+$)|(^\d+\.\d+$)/;
                    if (!anum.test(elemento.value))
                    {
                        alert("<?php echo $LDCampo; ?>" + elemento.name  + "<?php echo $LDNumerico; ?>");
                        elemento.focus();
                        return false;
                    }
                }

                function checklength(elemento)
                {
		  if (elemento.value.length > 24) {
		    elemento.value = elemento.value.substring(0, 24);
                    }
                }

                function chkform(d) {
                        if (d.ficha.value == "examen_fisico")
                        {
                            alert("<?php echo $LDPatologicError; ?>");
                        }
//                     if (d.fecha)
//                     {
//                         if (d.fecha.value == '')
//                         {
//                             alert("<?php echo $LDFechaError; ?>");
//                             d.fecha.focus;
//                             return false;
//                         }
//                     }
                    <?php
                    foreach ($oblig as $o)
                    {
			echo "if (d.$o.tagName) { if (d.$o.value == '') {alert('".$LDCampo." . ${'LD' . $o} . ".$LDObligatorio."'); d.$o.focus; return false; }}";

			echo "else { var ya = false; if (d.".$o."[0].checked) { ya = true; } if (d.".$o."[1].checked) { ya = true; } if (d.".$o."[2]) { if (d.".$o."[2].checked) { ya = true; } } if (d.".$o."[3]) { if (d.".$o."[3].checked) { ya = true; } } if (!ya) { alert('".$LDCampo." . ${'LD' . $o} . ".$LDObligatorio."'); d.$o.focus; return false; } }";
		    }
                    ?>
                    return true;
                }
                </script>
<?php
$sTemp = ob_get_contents();
ob_end_clean();
//echo $sTemp;
# Start Smarty templating here
/**
* LOAD Smarty
*/

# Note: it is advisable to load this after the inc_front_chain_lang.php so
# that the smarty script can use the user configured template theme

require_once($root_path.'gui/smarty_template/smarty_care.class.php');
$smarty = new smarty_care('common');
$smarty->assign('sRegFormJavaScript',$sTemp);
$smarty->assign('breakfile',$breakfile);
$smarty->assign('action',$action);
$smarty->assign('ficha',$ficha);
$smarty->assign('pid',$pid);

//se debe cargar el correlativo desde aqui
if (isset($ficha) && isset($sub)){
	$temp_guihistory = new GuiHistory($ficha);
	$temp_var = $temp_guihistory->getCorrBySubficha($ficha, $sub, $pid);
	$corr = $temp_var;
}
//var_dump($corr);

$smarty->assign('sub',$sub);

$smarty->assign('id_correlativo',$corr);

$smarty->assign('sToolbarTitle',$LDHC);

$smarty->assign('title',${'LD' . $ficha});
$smarty->assign('subtitle',${'LD' . $sub});

$tab_bot_line='#66ee66';
require('./gui_bridge/default/gui_tabs_patreg.php');


$lang_tables =array('person.php', 'interconsulta.php', 'history.php');
include($root_path.'include/inc_load_lang_tables.php');

ob_start();
require('./gui_bridge/default/gui_patient_sub_options.php');
$sTemp = ob_get_contents();
ob_end_clean();

//Importacion y creacion de instancia de la clase queries_historia_clinica.php
$filename = $root_path."db/historia_clinica/queries_historia_clinica.php";
require_once($filename);
$query_historia_clinica = new Queries_historia_clinica();


$smarty->assign('sHisOptions',$sTemp);

//var_dump($gui);
$smarty->assign('gui',$gui);

global $db;
global $_SESSION;
//var_dump($db);
//$sql='SELECT name, login_id, password, permission, lockflag FROM care_users WHERE login_id=\''.addslashes($_SESSION['sess_login_userid']).'\'';
$sql = $query_historia_clinica->select_care_historia_adjuntos($_SESSION['sess_login_userid']);

// buscado array ('login_id'=>\'''.addslashes($_SESSION['sess_login_userid']') 
// valores nuevos array (''=>,)

if($ergebnis=$db->SelectUserInfo($_SESSION['sess_login_userid']))
  {
    $zeile=$ergebnis;
    
    if (($zeile['permission'] == '_a_3_historiaauditor')||($zeile['permission'] == 'System_Admin'))
      {
	$auditor = true;
      } 
    else 
      {
	$auditor = false;
      }
    
  }

  //mensaje que señala que debe buscarse un paciente antes de suar las funcionalidades
  if( ($pid==NULL)) 
  $smarty->assign('title',$LDHistoEntra);
 



  //cargar los botones de guardar datos y borrar
	$ver_ficha=$_GET['ficha'];
  	if (((!$update) || ($auditor))&& isset($ver_ficha)) {
  		$smarty->assign('pbSubmit','<input type="image" '.createLDImgSrc($root_path,'savedisc.gif','0').'  alt="'.$LDSaveData.'" align="absmiddle">');
  		$smarty->assign('pbReset','<a href="javascript:document.regform.reset()"><img '.createLDImgSrc($root_path,'reset.gif','0').' alt="'.$LDResetData.'" align="absmiddle"></a>');
	 } else {
 	 $smarty->assign('pbSubmit','');
 	 $smarty->assign('pbReset','');
	 }  


$smarty->assign('sRegFormJavaScript',$sTemp2);

$smarty->assign('sMainBlockIncludeFile','historia_clinica/historia_clinica.tpl');

 /**
 * show Template
 */
$smarty->display('common/mainframe.tpl');
}else{
header("Location:".$root_path."main/startframe.php?noti=1&sid=$sid&lang=$lang");
exit;
}
?>
